package com.xgst.land.activity.check

import android.content.Intent
import android.content.res.Configuration
import android.graphics.Color
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.UiMessageUtils
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.serial.SerialPortUtils
import com.xgst.land.R
import com.xgst.land.bean.MessageScanBean
import com.xgst.land.databinding.ActivityPdDetailLayoutBinding
import com.xgst.land.utils.MessageKey
import com.xgst.land.utils.SoundUtils
import com.xgst.land.viewmodel.InventoryOrderDetailViewModel

//开始盘点，表单详情
class InventoryOrderDetailActivity : BaseActivity<ActivityPdDetailLayoutBinding>(),
    UiMessageUtils.UiMessageCallback {
    override fun viewBinding(): ActivityPdDetailLayoutBinding {
        return ActivityPdDetailLayoutBinding.inflate(layoutInflater)
    }

    private lateinit var model:InventoryOrderDetailViewModel
    private val adapter = PDOrderDetailAdapter()
    private var planId = 0

    override fun init() {
        planId = intent.getIntExtra("id",-1)
        model = ViewModelProvider(this)[InventoryOrderDetailViewModel::class.java]
        val title = intent.getStringExtra("title")?:"表单名称"
        setTitle(title)

        //开始盘点
        binding.btnStart.setOnClickListener {
            binding.btnStart.text = "识别中..."
            binding.btnStart.setTextColor(Color.RED)
            SerialPortUtils.startHF()
        }
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        binding.recyclerView.adapter = adapter

        UiMessageUtils.getInstance().addListener(MessageKey.SCAN_ID,this)
    }

    override fun onResume() {
        super.onResume()
        initData()
    }

    private fun initData() {
        model.reuqestSampleBox("$planId",{
            adapter.setNewInstance(it.inventorySampleBoxList)
            binding.tvCreateTime.text = "${it.inventoryTable.createTime}"
            binding.tvCreateName.text = "${it.inventoryTable.username}"
            val size = it.inventorySampleBoxList.filter { it.checkStatus == 1 }.size
            binding.tvProgress.text = "$size/${it.inventorySampleBoxList.size}"
            if(size == it.inventorySampleBoxList.size){
                binding.btnStart.isEnabled = false
                binding.btnStart.setBackgroundColor(Color.GRAY)
            }
        })
    }

    override fun handleMessage(localMessage: UiMessageUtils.UiMessage) {
        if(!ActivityUtils.getTopActivity().localClassName.contains("InventoryOrderDetailActivity")){
            return
        }
        val msgBean = localMessage.`object` as MessageScanBean
        binding.btnStart.setTextColor(Color.WHITE)
        if(msgBean.uid.isEmpty()){
            showToast("没有扫描到芯片uid")
            binding.btnStart.text = "开始盘点"
            return
        }
        //LogUtils.i("识别到UID  $msgBean.uid")
        showToast("识别成功，请稍后...")
        //SoundUtils.playSound(SoundUtils.SOUND_SUCCESS)
        var intent = Intent(this,InventoryScanLandActivity::class.java)
        intent.putExtra("uid",msgBean.uid) //E0040150A944AF6C
        intent.putExtra("orderId",planId)//2
        startActivityForResult(intent,{
            initData()
        })
        binding.btnStart.text = "开始盘点"
    }

    override fun onDestroy() {
        super.onDestroy()
        UiMessageUtils.getInstance().removeListener(this)
    }
}
